Kubernetes Native Microservices with Quarkus and MicroProfile by John Clingan & Ken Finnigan
Author:John Clingan & Ken Finnigan [Clingan, John & Finnigan, Ken]
Language: eng
Format: epub
Publisher: Manning Publications Co.
Published: 0101-01-01T00:00:00+00:00
7.8.4 Testing the circuit breaker
To test the circuit breaker, extend the WiremockAccountService with the following:
public class WiremockAccountService implements QuarkusTestResourceLifecycleManager { private WireMockServer wireMockServer; private static final String SERVER_ERROR_1 = "CB Fail 1"; ⶠprivate static final String SERVER_ERROR_2 = "CB Fail 2"; private static final String CB_OPEN_1 = "CB Open 1"; private static final String CB_OPEN_2 = "CB Open 2"; private static final String CB_OPEN_3 = "CB Open 3"; private static final String CB_SUCCESS_1 = "CB Success 1"; private static final String CB_SUCCESS_2 = "CB Success 2"; ... @Override public Map<String, String> start() { wireMockServer = new WireMockServer(); wireMockServer.start(); mockAccountService(); mockTimeout(); mockCircuitBreaker(); ⷠ.. } void mockCircuitBreaker() { // Define wiremock scenario to support the required by a circuitbreaker state machine createCircuitBreakerStub(Scenario.STARTED, SERVER_ERROR_1, "100.00", 200); ⸠createCircuitBreakerStub(SERVER_ERROR_1, SERVER_ERROR_2, "200.00", 502); ⹠createCircuitBreakerStub(SERVER_ERROR_2, CB_OPEN_1, "300.00", 502); ⺠createCircuitBreakerStub(CB_OPEN_1, CB_OPEN_2, "400.00", 200); ⻠createCircuitBreakerStub(CB_OPEN_2, CB_OPEN_3, "400.00", 200); createCircuitBreakerStub(CB_OPEN_3, CB_SUCCESS_1, "500.00", 200); ⼠createCircuitBreakerStub(CB_SUCCESS_1, CB_SUCCESS_2, "600.00", 200); ⽠} void createCircuitBreakerStub(String currentState, String nextState, ⾠String response, int status) { stubFor(post(urlEqualTo("/accounts/444666/transaction")).inScenario("circuitbreaker") .whenScenarioStateIs(currentState).willSetStateTo(nextState).willReturn( aResponse().withStatus(status).withHeader("Content-Type", MediaType.TEXT_PLAIN).withBody(response))); } ...
â¶ The states defined for the circuitbreaker WireMock âscenario.â Each field defines a circuit breaker state in order.
â· Creates the circuit breaker mock
⸠Creates a WireMock circuit breaker stub for each scenario state transition. This first stub defines the initial request in the requestVolumeThreshold.
â¹ Returns a 502, the first error the circuit breaker receives
⺠Returns a 502, the second error the circuit breaker receives. This second error will open the circuit breaker.
â» The circuit breaker is open. Even though the request returns 200, simulating service availability, the circuit breaker is in its delay period.
â¼ The first successful call after the delay period
â½ The second successful call closes the circuit.
â¾ Any call to the /accounts/444666/transaction endpoint invokes a stub. Each call to the endpoint will advance the state in the circuitbreaker scenario. The body of the response is the account balance.
With the WiremockAccountService updated to support a circuit breaker, the next step is to update FaultyAccountService to test the circuit breaker, as follows.
Listing 7.17 Circuit breaker JUnit test
@Test void testCircuitBreaker() { RequestSpecification request = given() .body("142.12") .contentType(ContentType.JSON); request.post("/transactions/api/444666").then().statusCode(200); ⶠrequest.post("/transactions/api/444666").then().statusCode(502); ⷠrequest.post("/transactions/api/444666").then().statusCode(502); ⸠request.post("/transactions/api/444666").then().statusCode(503); ⹠request.post("/transactions/api/444666").then().statusCode(503); ⺠try { TimeUnit.MILLISECONDS.sleep(1000); ⻠} catch (InterruptedException e) { } request.post("/transactions/api/444666").then().statusCode(200); ⼠request.post("/transactions/api/444666").then().statusCode(200); ⽠}
â¶ This successful request defines the initial request in the requestVolumeThreshold window.
â· Expects a 502, the first error the circuit breaker receives
⸠Expects a 502, the second error the circuit breaker receives. This request will open the circuit breaker.
â¹ The circuit breaker is open.
⺠The circuit breaker is still open.
â» Sleeps long enough to get past the circuitbreaker delay
â¼ The first successful call after the delay period
â½ The second successful call closes the circuit. The circuit is now closed, and further invocations will continue normally.
Note Early Quarkus releases used the Hystrix framework as the underlying implementation. Hystrix has been deprecated, so later Quarkus releases use a custom implementation. Because developers develop to the MicroProfile Fault Tolerance specification, their application source code did not change. This demonstrates the real-world value of developing to specifications instead of implementations.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Exploring Deepfakes by Bryan Lyon and Matt Tora(7712)
Robo-Advisor with Python by Aki Ranin(7611)
Offensive Shellcode from Scratch by Rishalin Pillay(6096)
Microsoft 365 and SharePoint Online Cookbook by Gaurav Mahajan Sudeep Ghatak Nate Chamberlain Scott Brewster(5009)
Ego Is the Enemy by Ryan Holiday(4956)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4438)
Python for ArcGIS Pro by Silas Toms Bill Parker(4174)
Elevating React Web Development with Gatsby by Samuel Larsen-Disney(3877)
Machine Learning at Scale with H2O by Gregory Keys | David Whiting(3613)
Learning C# by Developing Games with Unity 2021 by Harrison Ferrone(3284)
Speed Up Your Python with Rust by Maxwell Flitton(3231)
Liar's Poker by Michael Lewis(3220)
OPNsense Beginner to Professional by Julio Cesar Bueno de Camargo(3195)
Extreme DAX by Michiel Rozema & Henk Vlootman(3169)
Agile Security Operations by Hinne Hettema(3122)
Linux Command Line and Shell Scripting Techniques by Vedran Dakic and Jasmin Redzepagic(3108)
Essential Cryptography for JavaScript Developers by Alessandro Segala(3082)
Cryptography Algorithms by Massimo Bertaccini(3001)
AI-Powered Commerce by Andy Pandharikar & Frederik Bussler(2981)
